package com.xteam.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public final class WebTools {

	private WebTools() {
	}

	public static Map<String, String> getParamtemerToMap(HttpServletRequest req) {
		Map<String, String> params = new HashMap<String, String>();
		Enumeration<?> enums = req.getParameterNames();
		while (enums.hasMoreElements()) {
			String key = (String) enums.nextElement();
			params.put(key, req.getParameter(key));
		}
		return params;
	}

	public static Map<String, String[]> getParamtemerValuesToMap(HttpServletRequest req) {
		Map<String, String[]> params = new HashMap<String, String[]>();
		Enumeration<?> enums = req.getParameterNames();
		while (enums.hasMoreElements()) {
			String key = (String) enums.nextElement();
			params.put(key, req.getParameterValues(key));
		}
		return params;
	}

	/**
	 * 响应客户端请求 并且返回json数据
	 * @param response
	 * @param o
	 * @throws IOException
	 * @author yuanjie.wu
	 */
	public static void writerJsonToResponse(HttpServletResponse response, Object o) throws IOException {
		PrintWriter out = response.getWriter();
		JSONObject json	= JSONObject.fromObject(o);
		out.print(json.toString());
	}
}
